package com.changanw.tienchin.web.controller.tienchin;


import com.changanw.tienchin.activity.domain.vo.ActivityVo;
import com.changanw.tienchin.activity.service.IActivityService;
import com.changanw.tienchin.common.validator.CreateGroup;
import com.changanw.tienchin.common.validator.EditGroup;
import com.changanw.tienchin.channel.domain.vo.ChannelVo;
import com.changanw.tienchin.channel.service.IChannelService;
import com.changanw.tienchin.common.annotation.Log;
import com.changanw.tienchin.common.core.controller.BaseController;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.core.page.TableDataInfo;
import com.changanw.tienchin.common.enums.BusinessType;
import com.changanw.tienchin.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author changanw
 * @since 2023-11-21
 */
@RestController
@RequestMapping("/tienchin/activity")
public class ActivityController extends BaseController {
  @Autowired
  IActivityService activityService;
  @Autowired
  IChannelService channelService;
  @PreAuthorize("hasPermission('tienchin:activity:list')")
  @GetMapping("/list")
  public TableDataInfo list(ActivityVo activityVo) {
    startPage();
    List<ActivityVo> list = activityService.selectChannelList(activityVo);
    return getDataTable(list);
  }
  /**
   * 查询渠道
   */
  @PreAuthorize("hasPermission('tienchin:activity:list')")
  @GetMapping("/channel/list")
  public AjaxResult channelList() {
    return AjaxResult.success(channelService.selectChannelList(new ChannelVo()));
  }
  /**
   * 新增活动
   */
  @PreAuthorize("hasPermission('tienchin:activity:create')")
  @Log(title = "活动管理", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated(CreateGroup.class) @RequestBody ActivityVo activityVo) {
    return activityService.addActivity(activityVo);
  }
  /**
   * 修改活动
   */
  @PreAuthorize("hasPermission('tienchin:activity:edit')")
  @Log(title = "活动管理", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated(EditGroup.class) @RequestBody ActivityVo activityVo) {
    return activityService.updateChannel(activityVo);
  }

  /**
   * 根据活动id获取 活动信息
   * @param channelId
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:activity:list')")
  @GetMapping("/{activityId}")
  public AjaxResult getInfo(@PathVariable Long activityId){
    return AjaxResult.success(activityService.getActivityById(activityId));
  }
  /**
   * 删除活动
   */
  @PreAuthorize("hasPermission('tienchin:activity:remove')")
  @Log(title = "渠道管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/{activityIds}")
  public AjaxResult remove(@PathVariable Long[] activityIds) {
    return toAjax(activityService.deleteChannelByIds(activityIds));
  }
  /**
   * 活动导出
   * @param response
   * @param role
   */
  @Log(title = "活动管理", businessType = BusinessType.EXPORT)
  @PreAuthorize("hasPermission('tienchin:activity:export')")
  @PostMapping("/export")
  public void export(HttpServletResponse response, ActivityVo activityVo) {
    List<ActivityVo> list = activityService.selectChannelList(activityVo);
    ExcelUtil<ActivityVo> util = new ExcelUtil<ActivityVo>(ActivityVo.class);
    util.exportExcel(response, list, "活动数据");
  }
}
